home *** CD-ROM | disk | FTP | other *** search
/ Collection of Internet / Collection of Internet.iso / msdos / lynx / source / www / library / implemen / vms / htvmsuti.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-25  |  8.4 KB  |  350 lines

  1.  
  2. /* MODULE                            HTVMSUtil.c
  3. **        VMS Utility Routines
  4. **
  5. ** AUTHORS:
  6. **    MD    Mark Donszelmann    duns@vxdeop.cern.ch
  7. **
  8. ** HISTORY:
  9. **    14 Nov 93  MD    Written
  10. **
  11. ** BUGS:
  12. **
  13. **
  14. */
  15.  
  16. #include <ssdef.h>
  17. #include <jpidef.h>
  18. #include <prvdef.h>
  19. #include <acldef.h>
  20. #include <chpdef.h>
  21. #include <descrip.h>
  22.  
  23. #include <unixlib.h>
  24. #include <stdio.h>
  25.  
  26. #include "HTUtils.h"
  27. #include "HTVMSUtils.h"
  28.  
  29. #define INFINITY 512            /* file name length @@ FIXME */
  30.  
  31.  
  32. typedef struct {
  33.    unsigned BufferLength : 16;
  34.    unsigned ItemCode : 16;
  35.    unsigned BufferAddress : 32;
  36.    unsigned ReturnLengthAddress : 32;
  37. } ItemStruct;
  38.  
  39. /* PUBLIC                            HTVMS_authSysPrv()
  40. **        CHECKS IF THIS PROCESS IS AUTHORIZED TO ENABLE SYSPRV
  41. ** ON ENTRY:
  42. **    No arguments.
  43. **
  44. ** ON EXIT:
  45. **    returns    YES if SYSPRV is authorized
  46. */
  47. PUBLIC BOOL HTVMS_authSysPrv NOARGS
  48. {
  49. unsigned long Result;
  50. ItemStruct ItemList[2];
  51. unsigned long Length;
  52. unsigned long Buffer[2];
  53.  
  54.   /* fill Item */
  55.   ItemList[0].BufferLength = sizeof(Buffer);
  56.   ItemList[0].BufferAddress = Buffer;
  57.   ItemList[0].ReturnLengthAddress = &Length;
  58.   ItemList[0].ItemCode = JPI$_AUTHPRIV;
  59.  
  60.   /* terminate list */
  61.   ItemList[1].ItemCode = 0;
  62.   ItemList[1].BufferLength = 0;
  63.  
  64.   /* call system */
  65.   Result = SYS$GETJPIW(0, 0, 0, ItemList, 0, 0, 0);
  66.  
  67.   if (Result != SS$_NORMAL)
  68.      return(NO);  
  69.  
  70.   if (Buffer[0] & PRV$M_SYSPRV)
  71.      return(YES);
  72.  
  73.   return(NO);  
  74. }
  75.  
  76.  
  77.  
  78. /* PUBLIC                            HTVMS_enableSysPrv()
  79. **        ENABLES SYSPRV
  80. ** ON ENTRY:
  81. **    No arguments.
  82. **
  83. ** ON EXIT:
  84. **    
  85. */
  86. PUBLIC void HTVMS_enableSysPrv NOARGS
  87. {
  88. unsigned long Result;
  89. unsigned long Prv[2], PreviousPrv[2];
  90.  
  91.    Prv[0] = PRV$M_SYSPRV;
  92.    Prv[1] = 0;
  93.    Result = SYS$SETPRV(1,&Prv,0,&PreviousPrv);
  94.  
  95.    if (TRACE) {
  96.       if (Result == SS$_NORMAL) {
  97.          if (!(PreviousPrv[0] & PRV$M_SYSPRV)) {
  98.             fprintf(stderr, "HTVMS_enableSysPrv: Enabled SYSPRV\n");
  99.          }
  100.       }
  101.    }
  102. }
  103.  
  104.  
  105.  
  106. /* PUBLIC                            HTVMS_disableSysPrv()
  107. **        DISABLES SYSPRV
  108. ** ON ENTRY:
  109. **    No arguments.
  110. **
  111. ** ON EXIT:
  112. **    
  113. */
  114. PUBLIC void HTVMS_disableSysPrv NOARGS
  115. {
  116. unsigned long Result;
  117. unsigned long Prv[2], PreviousPrv[2];
  118.  
  119.    Prv[0] = PRV$M_SYSPRV;
  120.    Prv[1] = 0;
  121.    Result = SYS$SETPRV(0,&Prv,0,&PreviousPrv);
  122.  
  123.    if (TRACE) {
  124.       if (Result == SS$_NORMAL) {
  125.          if (PreviousPrv[0] & PRV$M_SYSPRV) {
  126.             fprintf(stderr, "HTVMS_disableSysPrv: Disabled SYSPRV\n");
  127.          }
  128.       }
  129.    }
  130. }
  131.  
  132.  
  133.  
  134. /* PUBLIC                            HTVMS_checkAccess()
  135. **        CHECKS ACCESS TO FILE FOR CERTAIN USER
  136. ** ON ENTRY:
  137. **    FileName    The file to be accessed
  138. **    UserName    Name of the user to check access for.
  139. **            User nobody, represented by "" is given NO for an answer
  140. **    Method        Name of the method to be chceked
  141. **
  142. ** ON EXIT:
  143. **    returns YES if access is allowed
  144. **    
  145. */
  146. PUBLIC BOOL HTVMS_checkAccess ARGS3(
  147.     CONST char *, FileName,
  148.     CONST char *, UserName,
  149.     CONST char *, Method)
  150. {
  151. unsigned long Result;
  152. ItemStruct ItemList[2];
  153. unsigned long Length;
  154. unsigned long Buffer;
  155. unsigned long ObjType;
  156.  
  157. char *VmsName;
  158.  
  159. struct dsc$descriptor_s FileNameDesc;
  160. struct dsc$descriptor_s UserNameDesc;
  161.  
  162. char *colon;
  163.  
  164.    /* user nobody should access as from account under which server is running */
  165.    if (0 == strcmp(UserName,""))
  166.       return(NO);
  167.  
  168.    /* check Filename and convert */
  169.    colon = strchr(FileName,':');
  170.    if (colon)
  171.       VmsName = HTVMS_name("",colon+1);
  172.    else
  173.       VmsName = HTVMS_name("",FileName);
  174.  
  175.    /* check for GET */
  176.    if (0 == strcmp(Method,"GET"))
  177.    {
  178.      /* fill Item */
  179.      ItemList[0].BufferLength = sizeof(Buffer);
  180.      ItemList[0].BufferAddress = &Buffer;
  181.      ItemList[0].ReturnLengthAddress = &Length;
  182.      ItemList[0].ItemCode = CHP$_FLAGS;
  183.  
  184.      /* terminate list */
  185.      ItemList[1].ItemCode = 0;
  186.      ItemList[1].BufferLength = 0;
  187.  
  188.      /* fill input */
  189.      ObjType = ACL$C_FILE;
  190.      Buffer = CHP$M_READ;
  191.      UserNameDesc.dsc$w_length = strlen(UserName);
  192.      UserNameDesc.dsc$b_dtype = DSC$K_DTYPE_T;
  193.      UserNameDesc.dsc$b_class = DSC$K_CLASS_S;
  194.      UserNameDesc.dsc$a_pointer = UserName;
  195.      FileNameDesc.dsc$w_length = strlen(VmsName);
  196.      FileNameDesc.dsc$b_dtype = DSC$K_DTYPE_T;
  197.      FileNameDesc.dsc$b_class = DSC$K_CLASS_S;
  198.      FileNameDesc.dsc$a_pointer = VmsName;
  199.  
  200.      /* call system */
  201.      Result = SYS$CHECK_ACCESS(&ObjType,&FileNameDesc,&UserNameDesc,ItemList);
  202.  
  203.      if (Result == SS$_NORMAL)
  204.         return(YES);
  205.      else
  206.         return(NO);
  207.    }
  208.  
  209.    return(NO);
  210. }
  211.  
  212.  
  213.  
  214.  
  215. /* PUBLIC                            HTVMS_wwwName()
  216. **        CONVERTS VMS Name into WWW Name 
  217. ** ON ENTRY:
  218. **    vmsname        VMS file specification (NO NODE)
  219. **
  220. ** ON EXIT:
  221. **    returns     www file specification
  222. **
  223. ** EXAMPLES:
  224. **    vmsname                wwwname
  225. **    DISK$USER             disk$user
  226. **    DISK$USER:             /disk$user/
  227. **    DISK$USER:[DUNS]         /disk$user/duns
  228. **    DISK$USER:[DUNS.ECHO]         /disk$user/duns/echo
  229. **    [DUNS]                 duns
  230. **    [DUNS.ECHO]             duns/echo
  231. **    [DUNS.ECHO.-.TRANS]         duns/echo/../trans
  232. **    [DUNS.ECHO.--.TRANS]         duns/echo/../../trans
  233. **    [.DUNS]             duns
  234. **    [.DUNS.ECHO]             duns/echo
  235. **    [.DUNS.ECHO]TEST.COM         duns/echo/test.com 
  236. **    TEST.COM             test.com
  237. **
  238. **    
  239. */
  240. PUBLIC char * HTVMS_wwwName ARGS1(
  241.     char *, vmsname)
  242. {
  243. static char wwwname[256];
  244. char *src, *dst;
  245. int dir;
  246.    dst = wwwname;
  247.    src = vmsname;
  248.    dir = 0;
  249.    if (strchr(src,':')) *(dst++) = '/';
  250.    for ( ; *src != '\0' ; src++)
  251.    {
  252.       switch(*src)
  253.       {
  254.          case ':':  *(dst++) = '/'; break;
  255.          case '-': if (*(src-1) == '-') *(dst++) = '/';
  256.                    *(dst++) = '.'; 
  257.                    *(dst++) = '.'; 
  258.                    break;
  259.          case '.': if (dir)
  260.                    {
  261.                       if (*(src-1) != '[') *(dst++) = '/';
  262.                    }
  263.                    else
  264.                       *(dst++) = '.';
  265.                    break;
  266.          case '[': dir = 1; break;
  267.          case ']': dir = 0; break;
  268.          default:  if (*(src-1) == ']') *(dst++) = '/';
  269.                    *(dst++) = *src; 
  270.                    break;
  271.       }
  272.    }
  273.    *(dst++) = '\0';
  274.    return(wwwname);
  275. }
  276.  
  277.  
  278. /* PUBLIC                            HTVMS_name()
  279. **        CONVERTS WWW name into a VMS name
  280. ** ON ENTRY:
  281. **    nn        Node Name (optional)
  282. **    fn        WWW file name
  283. **
  284. ** ON EXIT:
  285. **    returns     vms file specification
  286. **
  287. ** Bug:    Returns pointer to static -- non-reentrant
  288. */
  289. PUBLIC char * HTVMS_name ARGS2(
  290.     CONST char *, nn, 
  291.     CONST char *, fn)
  292. {
  293.  
  294. /*    We try converting the filename into Files-11 syntax. That is, we assume
  295. **    first that the file is, like us, on a VMS node. We try remote
  296. **    (or local) DECnet access. Files-11, VMS, VAX and DECnet
  297. **    are trademarks of Digital Equipment Corporation. 
  298. **    The node is assumed to be local if the hostname WITHOUT DOMAIN
  299. **    matches the local one. @@@
  300. */
  301.     static char vmsname[INFINITY];    /* returned */
  302.     char * filename = (char*)malloc(strlen(fn)+1);
  303.     char * nodename = (char*)malloc(strlen(nn)+2+1);    /* Copies to hack */
  304.     char *second;        /* 2nd slash */
  305.     char *last;            /* last slash */
  306.     
  307.     char * hostname = HTHostName();
  308.  
  309.     if (!filename || !nodename) outofmem(__FILE__, "HTVMSname");
  310.     strcpy(filename, fn);
  311.     strcpy(nodename, "");    /* On same node? Yes if node names match */
  312.     {
  313.         char *p, *q;
  314.         for (p=hostname, q=nn; *p && *p!='.' && *q && *q!='.'; p++, q++){
  315.         if (TOUPPER(*p)!=TOUPPER(*q)) {
  316.             strcpy(nodename, nn);
  317.         q = strchr(nodename, '.');    /* Mismatch */
  318.         if (q) *q=0;            /* Chop domain */
  319.         strcat(nodename, "::");        /* Try decnet anyway */
  320.         break;
  321.         }
  322.     }
  323.     }
  324.  
  325.     second = strchr(filename+1, '/');        /* 2nd slash */
  326.     last = strrchr(filename, '/');    /* last slash */
  327.         
  328.     if (!second) {                /* Only one slash */
  329.     sprintf(vmsname, "%s%s", nodename, filename + 1);
  330.     } else if(second==last) {        /* Exactly two slashes */
  331.     *second = 0;        /* Split filename from disk */
  332.     sprintf(vmsname, "%s%s:%s", nodename, filename+1, second+1);
  333.     *second = '/';    /* restore */
  334.     } else {                 /* More than two slashes */
  335.     char * p;
  336.     *second = 0;        /* Split disk from directories */
  337.     *last = 0;        /* Split dir from filename */
  338.     sprintf(vmsname, "%s%s:[%s]%s",
  339.         nodename, filename+1, second+1, last+1);
  340.     *second = *last = '/';    /* restore filename */
  341.     for (p=strchr(vmsname, '['); *p!=']'; p++)
  342.         if (*p=='/') *p='.';    /* Convert dir sep.  to dots */
  343.     }
  344.     free(nodename);
  345.     free(filename);
  346.     return vmsname;
  347. }
  348.  
  349.  
  350.